﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CartoGraphic.Model2;
using CartoGraphic.Model;
using System.IO;
using System.Xml.Serialization;
using System.Collections;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;

namespace CartoGraphic.MyTool
{
    class IOBase
    {
        public IOBase()
        { }

        DataFrame frame;
        public static Dictionary<string, string> symbolCode;  //编码索引
        public static Dictionary<string, string> infoCode;

        #region 属性
        public DataFrame Frame
        {
            get { return frame; }
            set { frame = value; }
        }

        public Dictionary<string, string> InfoCode
        {
            get { return infoCode; }
            set { infoCode = value; }
        }

        public Dictionary<string, string> SymbolCode
        {
            get { return symbolCode; }
            set { symbolCode = value; }
        }
        #endregion

        #region 读取1
        public List<SimplePoint> readPoint(StreamReader sr, int pointCount)
        {
            List<SimplePoint> points = new List<SimplePoint>();
            //List<PointGeo> points = new List<PointGeo>();
            string s = "", codeInfo = "";
            double x, y;
            for (int i = 0; i < pointCount * 2; i++)
            {
                s = sr.ReadLine();
                if (i % 2 == 0)
                {
                    codeInfo = s.Trim();
                }
                if (i % 2 == 1)
                {
                    string[] locations = s.Split(',');
                    x = Double.Parse(locations[0]);
                    y = Double.Parse(locations[1]);
                    SimplePoint geo = new SimplePoint(frame.transPoint(x, y), infoCode[codeInfo]);
                    geo.Code = codeInfo;
                    geo.Type = symbolCode[codeInfo];
                    geo.Dimension = "点";
                    //SimplePoint geo = new PointGeo(new MyPoint(x, y), symbolCode[codeInfo]);
                    geo.Info = infoCode[codeInfo];
                    points.Add(geo);
                }
            }
            return points;
        }

        public List<PolyLine> readLines(StreamReader sr, int lineCount)
        {
            List<PolyLine> lines = new List<PolyLine>();
            string s = "";
            for (int i = 0; i < lineCount; i++)
            {
                PolyLine line = new PolyLine();
                //List<MyPoint> linepoints = new List<MyPoint>();
                s = sr.ReadLine();
                string codeinfo = s.Trim();
                s = sr.ReadLine();
                int pointCount = Int16.Parse(s.Trim());
                for (int j = 0; j < pointCount; j++)
                {
                    s = sr.ReadLine();
                    double x = Double.Parse(s.Split(',')[0].Trim());
                    double y = Double.Parse(s.Split(',')[1].Trim());
                    line.AddPoint(frame.transPoint(x, y));
                    //linepoints.Add(new MyPoint(x, y));
                }
                line.Code = codeinfo;
                line.Info = infoCode[codeinfo];
                //PolyLineGeo line = new PolyLineGeo(linepoints);
                line.Type = symbolCode[codeinfo];
                //line.info = infoCode[codeinfo];
                line.Dimension = "线";
                lines.Add(line);
                sr.ReadLine();
            }
            return lines;
        }

        public List<Polygon> readPolygon(StreamReader sr, int polygonCount)
        {
            List<Polygon> lines = new List<Polygon>();
            string s = "";
            for (int i = 0; i < polygonCount; i++)
            {
                Polygon polygon = new Polygon();
                //List<MyPoint> linepoints = new List<MyPoint>();
                s = sr.ReadLine();
                string codeinfo = s.Trim();
                s = sr.ReadLine();
                int pointCount = Int16.Parse(s.Trim());
                for (int j = 0; j < pointCount; j++)
                {
                    s = sr.ReadLine();
                    double x = Double.Parse(s.Split(',')[0].Trim());
                    double y = Double.Parse(s.Split(',')[1].Trim());
                    polygon.AddPoint(frame.transPoint(x, y));
                    //linepoints.Add(new MyPoint(x, y));
                }
                //PolygonGeo line = new PolygonGeo(linepoints);
                polygon.Info = infoCode[codeinfo];
                polygon.Code = codeinfo;
                polygon.Type = symbolCode[codeinfo];
                polygon.Dimension = "面";
                //line.info = infoCode[codeinfo];
                lines.Add(polygon);
                sr.ReadLine();
            }
            return lines;
        }
        #endregion

        #region 读取2
        public SimplePoint openPoint(StreamReader sr)
        {
            string code = sr.ReadLine().Trim();
            string location = sr.ReadLine().Trim();
            string[] s = location.Split(' ');
            int X = Int16.Parse(s[0]);
            int Y = Int16.Parse(s[1]);
            SimplePoint point = new SimplePoint(new Point(X, Y), infoCode[code]);
            point.Code = code;
            point.Dimension = "点";
            point.Type = SymbolCode[code];
            return point;
        }

        public PolyLine openLine(StreamReader sr)
        {
            PolyLine line = new PolyLine();
            string code = sr.ReadLine().Trim();
            string count_string = sr.ReadLine().Trim();
            int count = Int16.Parse(count_string);
            for (int i = 0; i < count; i++)
            {
                string location=sr.ReadLine().Trim();
                string[] s = location.Split(' ');
                int X = Int16.Parse(s[0]);
                int Y = Int16.Parse(s[1]);
                Point point = new Point(X, Y);
                line.AddPoint(point);
            }
            line.Code = code;
            line.Dimension = "线";
            line.Info = infoCode[code];
            line.Type = SymbolCode[code];
            return line;
        }

        public Polygon openPolygon(StreamReader sr)
        {
            Polygon line = new Polygon();
            string code = sr.ReadLine().Trim();
            string count_string = sr.ReadLine().Trim();
            int count = Int16.Parse(count_string);
            for (int i = 0; i < count; i++)
            {
                string location = sr.ReadLine().Trim();
                string[] s = location.Split(' ');
                int X = Int16.Parse(s[0]);
                int Y = Int16.Parse(s[1]);
                Point point = new Point(X, Y);
                line.AddPoint(point);
            }
            line.Code = code;
            line.Dimension = "面";
            line.Info = infoCode[code];
            line.Type = SymbolCode[code];
            return line;
        }
        #endregion

        public static string Serializer(Type type, object obj)
        {
            MemoryStream Stream = new MemoryStream();
            XmlSerializer xml = new XmlSerializer(type);
            try
            {
                xml.Serialize(Stream, obj);
            }
            catch (InvalidOperationException)
            {
                throw;
            }
            Stream.Position = 0;
            StreamReader sr = new StreamReader(Stream);
            string str = sr.ReadToEnd();
            sr.Dispose();
            Stream.Dispose();
            return str;
        }

        public static object Deserialize(Type type, string xml) 
        { 
            try 
            { 
                using (StringReader sr = new StringReader(xml)) 
                {
                    XmlSerializer xmldes = new XmlSerializer(type); 
                    return xmldes.Deserialize(sr); 
                } 
            } catch (Exception e) 
            {
                return null;
            }
        }

        public static void writePoint(StreamWriter wr, SimplePoint point)
        {
            wr.WriteLine("Point");
            wr.WriteLine(point.Code);
            wr.WriteLine(point.CenterPoint.X + " " + point.CenterPoint.Y);
        }

        public static void writePolyLine(StreamWriter wr, PolyLine line)
        {
            wr.WriteLine("PolyLine");
            wr.WriteLine(line.Code);
            wr.WriteLine(line.PointArray.Count);
            ArrayList points=line.PointArray;
            for (int i = 0; i < line.PointArray.Count; i++)
            {
                Point one = (Point)points[i];
                wr.WriteLine(one.X + " " + one.Y);
            }           
        }

        public static void writePolygon(StreamWriter wr, Polygon polygon)
        {
            wr.WriteLine("Polygon");
            wr.WriteLine(polygon.Code);
            wr.WriteLine(polygon.PointArray.Count);
            ArrayList points = polygon.PointArray;
            for (int i = 0; i < polygon.PointArray.Count; i++)
            {
                Point one = (Point)points[i];
                wr.WriteLine(one.X + " " + one.Y);
            }
        }

        public static void ExportToCSV(System.Data.DataTable dt, string savaPath)
        {
            //string strPath = Path.GetTempPath() + strName + ".csv";//保存到本项目文件夹下

            string strPath = savaPath;//保存到指定目录下

            if (File.Exists(strPath))
            {
                File.Delete(strPath);
            }
            //先打印标头
            StringBuilder strColu = new StringBuilder();
            StringBuilder strValue = new StringBuilder();
            int i = 0;
            try
            {
                StreamWriter sw = new StreamWriter(new FileStream(strPath, FileMode.CreateNew), Encoding.GetEncoding("GB2312"));
                for (i = 0; i <= dt.Columns.Count - 1; i++)
                {
                    strColu.Append(dt.Columns[i].ColumnName);
                    strColu.Append(",");
                }
                strColu.Remove(strColu.Length - 1, 1);//移出掉最后一个,字符
                sw.WriteLine(strColu);
                foreach (DataRow dr in dt.Rows)
                {
                    strValue.Remove(0, strValue.Length);//移出
                    for (i = 0; i <= dt.Columns.Count - 1; i++)
                    {
                        strValue.Append(dr[i].ToString());
                        strValue.Append(",");
                    }
                    strValue.Remove(strValue.Length - 1, 1);//移出掉最后一个,字符
                    sw.WriteLine(strValue);
                }
                sw.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            //System.Diagnostics.Process.Start(strPath);
        }


    }

}
